import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ModalDirective} from "ngx-bootstrap";
import {AliyunOssOutputUrl, tuikeSystemUrl} from "../../../config/api";
import {HttpService} from "../../lib/service/http.service";
import {Headers, RequestOptions} from "@angular/http";
import * as moment from 'moment';
import {Router} from "@angular/router";

@Component({
  selector: 'spread',
  templateUrl: './spread.component.html',
  styleUrls: ['./spread.component.scss']
})
export class SpreadComponent implements OnInit {
  _startDate=null;
  _endDate=null;
  orderByShow="";
  orderBy="";
  isVisible=false;
  taskList=[];
  taskTotal=0;
  taskNameShow="";
  taskName="";
  taskPage=1;
  taskPageSize=10;
  second_category="";
  first_category="";
  second_category1="";
  first_category1="";
  typeValue="";
  endTime="";
  startTime="";
  task_id=0;

  realname="";
  mobile="";
  alipayAccount="";
  realnameShow="";
  mobileShow="";
  alipayAccountShow="";
  withDrawList=[];
  tuikeSystemUrl=tuikeSystemUrl;
  page=1;
  pageSize=20;
  total=0;
  _isSpinning=false;
  constructor(private httpService: HttpService, private el: ElementRef, private router: Router) {

  }
  showModal = () => {
    this.isVisible = true;
    this.taskPage=1;
    this.first_category="";
    this.second_category="";
    this.first_category1="";
    this.second_category1="";
    this.taskList=[];
    this.taskTotal=0;
    this.taskNameShow="";
    this.taskName="";
    this.getTaskList();
  }

  handleOk = (e) => {
    console.log('点击了确定');
    this.isVisible = false;
  }

  handleCancel = (e) => {
    console.log(e);
    this.isVisible = false;
  }
  _startValueChange = () => {
    if (this._startDate > this._endDate) {
      this._endDate = null;
    }
  };
  _endValueChange = () => {
    if (this._startDate > this._endDate) {
      this._startDate = null;
    }
  };
  _disabledStartDate = (startValue) => {
    if (!startValue || !this._endDate) {
      return false;
    }
    return startValue.getTime() >= this._endDate.getTime();
  };
  _disabledEndDate = (endValue) => {
    if (!endValue || !this._startDate) {
      return false;
    }
    return endValue.getTime() <= this._startDate.getTime();
  };
  getDownUrl(){
    let url = `${tuikeSystemUrl}/api/v1/admin/Member/index?export=1`;
    if(!this.task_id&&!this.startTime&&!this.endTime){
      url+="&page="+this.page+"&pageSize="+this.pageSize;
    }else{
      if(this.task_id){
        url+="&task_id="+this.task_id;
      }
      if(this.startTime){
        url+="&startTime="+this.startTime;
      }
      if(this.endTime){
        url+="&endTime="+this.endTime;
      }

    }
    if(this.orderBy){
      url+="&orderBy="+this.orderBy;
    }
    return url;
  }
  changeOrder(e){
    console.log(e);
    this.getList();
  }
  getList(){
    let url=`${tuikeSystemUrl}/api/v1/admin/Member/index?page=`+this.page+"&pageSize="+this.pageSize;
    if(this.task_id){
      url+="&task_id="+this.task_id;
    }
    if(this.startTime){
      url+="&startTime="+this.startTime;
    }
    if(this.endTime){
      url+="&endTime="+this.endTime;
    }
    if(this.orderBy){
      url+="&orderBy="+this.orderBy;
    }
    console.log(url);
    this._isSpinning=true;
    this.httpService.httpGetAuth(url).subscribe(res => {
      if(res.code === 200) {
        this.withDrawList=res.data.data;
        this.total=res.data.total;
      }
      this._isSpinning=false;
    });
  }
  second_categoryList={};
  first_categoryList=[];
  loadData(e:{option: any, index: number, resolve: Function, reject: Function}){
    if (e.index === -1) {//第一集
      if(this.first_categoryList.length==0){
        this.httpService.httpGetAuth(`${tuikeSystemUrl}/api/v1/admin/category/category_list?level=1&parent_id=0&pageSize=200`).subscribe(res=>{
          if(res.code==200){
            this.first_categoryList=res.data.map(item=>{
              return { value: item.id,label: item.category_name};
            });
            e.resolve(this.first_categoryList);
          }
        });
      }else{
        e.resolve(this.first_categoryList);
      }
      return;
    }


    if (e.index === 0) {//第二季
      const option = e.option;
      if(!this.second_categoryList[option.value] ){
        option.loading = true;
        let url=`${tuikeSystemUrl}/api/v1/admin/category/category_list?level=2&parent_id=`+option.value+`&pageSize=200`;
        this.httpService.httpGetAuth(url).subscribe(res=>{
          if(res.code==200){
            if (res.data.length==0) {
              option.isLeaf=true;
              e.resolve([]);
            }else{
              this.second_categoryList[option.value]=res.data.map(item=>{
                return { value: item.id,label: item.category_name,isLeaf: true};
              });
              e.resolve(this.second_categoryList[option.value]);
            }

          }
          option.loading = false;
        });
      }else{
        e.resolve(this.second_categoryList[option.value]);
      }

    }
    /*if (e.index === 1) {
      setTimeout(() => {
        option.loading = false;
        e.resolve(scenicspots[option.value]);
      }, 1000);
    }*/
  }
  _console(value){
    console.log(value);
  }
  changePage(e){
    console.log('e:',e);
    this.taskPage=e;
    this.getTaskList();
  }
  checkId=0;
  checkId1=0;
  checkName="";
  checkName1="";
  _refreshStatus(e,item){
    console.log(e);
    if(e){
      this.checkId=item.id;
      this.checkName=item.task_name;
    }else{
      this.checkId=0;
      this.checkName="";
    }
  }
  getTaskList(){
    let url=`${tuikeSystemUrl}/api/v1/admin/Task/task_list?page=`+this.taskPage+"&pageSize="+this.taskPageSize;

    if (this.taskName) {
      url+="&task_name="+this.taskName;
    }

    if(this.first_category){
      url+="&first_category="+this.first_category;
    }

    if(this.second_category){
      url+="&second_category="+this.second_category;
    }

    this.httpService.httpGetAuth(url).subscribe(res => {
      if(res.code === 200) {
        this.taskList=res.data.list.data;
        this.taskTotal=res.data.list.total;
        console.log("total;",this.taskTotal);
      }
      //this._isSpinning=false;
    });
  }
  pageChanged(e){
    window.scrollTo(0, 0);
    console.log(e);
    this.page=e.page;
    this.getList();
  }
  ngOnInit() {
    this.getList();
  }
  reset(){
    this.task_id=0;
    this.startTime="";
    this.endTime="";
    this.checkName="";
    this._endDate=null;
    this._startDate=null;
    this.checkId=0;
    this.page=1;
    this.getList();
  }
  dateFtt(fmt,date)
  { //author: meizz
    var o = {
      "M+" : date.getMonth()+1,                 //月份
      "d+" : date.getDate(),                    //日
      "h+" : date.getHours(),                   //小时
      "m+" : date.getMinutes(),                 //分
      "s+" : date.getSeconds(),                 //秒
      "q+" : Math.floor((date.getMonth()+3)/3), //季度
      "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
      fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
      if(new RegExp("("+ k +")").test(fmt))
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
  }
  search(){
    this.task_id=this.checkId;
    if(this._startDate){
      this.startTime=this.dateFtt('yyyy-MM-dd hh:mm',this._startDate);
    }else{
      this.startTime="";
    }
    if(this._endDate){
      this.endTime=this.dateFtt('yyyy-MM-dd hh:mm',this._endDate);
    }else{
      this.endTime="";
    }
    this.page=1;
    this.getList();
  }
  search1(){
    this.taskPage=1;
    this.taskName=this.taskNameShow.trim();
    this.first_category=this.typeValue[0]||"";
    this.second_category=this.typeValue[1]||"";
    this.getTaskList();
  }
}
